package gw.com.android.ui.news;

import android.content.Context;
import android.view.View;
import android.widget.CheckedTextView;
import android.widget.RelativeLayout;

import com.gwtsz.gts2.hx.R;

import java.util.Calendar;

import www.com.library.util.StringFormatter;

/**
 * 日历Fragment 顶部的三个 日期处理
 * Created by hubble on 2017/4/25.
 */
public class CalendarTabDateViewHolder implements View.OnClickListener {

    // 三个日期tab
    private CheckedTextView mCtvWeekDay1;
    private CheckedTextView mCtvMonthDay1;

    private CheckedTextView mCtvWeekDay2;
    private CheckedTextView mCtvMonthDay2;

    private CheckedTextView mCtvWeekDay3;
    private CheckedTextView mCtvMonthDay3;

    private Calendar calendar1;
    private Calendar calendar2;
    private Calendar calendar3;

    private String[] weekDays;

    private OnSelectedDateListener listener;

    public CalendarTabDateViewHolder(Context context, View parent) {
        RelativeLayout mRlDate1 = (RelativeLayout) parent.findViewById(R.id.rl_date_1);
        mCtvWeekDay1 = (CheckedTextView) parent.findViewById(R.id.ctv_weekday_1);
        mCtvMonthDay1 = (CheckedTextView) parent.findViewById(R.id.ctv_monthday_1);

        RelativeLayout mRlDate2 = (RelativeLayout) parent.findViewById(R.id.rl_date_2);
        mCtvWeekDay2 = (CheckedTextView) parent.findViewById(R.id.ctv_weekday_2);
        mCtvMonthDay2 = (CheckedTextView) parent.findViewById(R.id.ctv_monthday_2);

        RelativeLayout mRlDate3 = (RelativeLayout) parent.findViewById(R.id.rl_date_3);
        mCtvWeekDay3 = (CheckedTextView) parent.findViewById(R.id.ctv_weekday_3);
        mCtvMonthDay3 = (CheckedTextView) parent.findViewById(R.id.ctv_monthday_3);

        weekDays = context.getResources().getStringArray(R.array.home_calendar_week);
        mRlDate1.setOnClickListener(this);
        mRlDate2.setOnClickListener(this);
        mRlDate3.setOnClickListener(this);
    }

    public void setOnSelectedDateListener(OnSelectedDateListener listener) {
        this.listener = listener;
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (R.id.rl_date_1 == id) {
            checkDate(0);
        } else if (R.id.rl_date_2 == id) {
            checkDate(1);
        } else if (R.id.rl_date_3 == id) {
            checkDate(2);
        }
    }

    public void updateDate(Calendar selected) {
        calendar1 = Calendar.getInstance();
        calendar1.setTime(selected.getTime());
        calendar2 = Calendar.getInstance();
        calendar2.setTime(calendar1.getTime());
        calendar2.add(Calendar.DAY_OF_MONTH, 1);
        calendar3 = Calendar.getInstance();
        calendar3.setTime(calendar2.getTime());
        calendar3.add(Calendar.DAY_OF_MONTH, 1);

        checkDate(0);

        mCtvWeekDay1.setText(getWeekDay(calendar1));
        mCtvMonthDay1.setText(getMonthDay(calendar1));

        mCtvWeekDay2.setText(getWeekDay(calendar2));
        mCtvMonthDay2.setText(getMonthDay(calendar2));

        mCtvWeekDay3.setText(getWeekDay(calendar3));
        mCtvMonthDay3.setText(getMonthDay(calendar3));
    }

    private void checkDate(int position) {
        if (position == -1) {
            return;
        }
        mCtvWeekDay1.setChecked(false);
        mCtvMonthDay1.setChecked(false);
        mCtvWeekDay2.setChecked(false);
        mCtvMonthDay2.setChecked(false);
        mCtvWeekDay3.setChecked(false);
        mCtvMonthDay3.setChecked(false);
        switch (position) {
            case 0:
                mCtvWeekDay1.setChecked(true);
                mCtvMonthDay1.setChecked(true);
                if (listener != null) {
                    listener.onSelectedDate(calendar1);
                }
                break;
            case 1:
                mCtvMonthDay2.setChecked(true);
                mCtvWeekDay2.setChecked(true);
                if (listener != null) {
                    listener.onSelectedDate(calendar2);
                }
                break;
            case 2:
                mCtvMonthDay3.setChecked(true);
                mCtvWeekDay3.setChecked(true);
                if (listener != null) {
                    listener.onSelectedDate(calendar3);
                }
                break;
        }
    }

    private String getWeekDay(Calendar calendar) {
        final int day = calendar.get(Calendar.DAY_OF_WEEK);
        return weekDays[day - 1];
    }

    private String getMonthDay(Calendar calendar) {
        return StringFormatter.instance().toDate(calendar.getTime()).substring(5);
    }

    public interface OnSelectedDateListener {
        void onSelectedDate(Calendar date);
    }

}
